 $(function(){

    var zzText = RegExp(/^(0|86|17951)?(13[0-9]|15[012356789]|17[3678]|18[0-9]|14[57])[0-9]{8}$/);

    /* 登录 */
    if(localStorage.getItem("login_photo") != null){
        $(".login_photo").attr("value",localStorage.getItem("login_photo"));
    }
    if(localStorage.getItem("login_pass") != null){
        $(".login_pass").attr("value",localStorage.getItem("login_pass"));
    }
    var login_photo = $(".login_photo").val();
    var login_pass = $(".login_pass").val();
    if((login_photo != "") && (zzText.test(login_photo) == true) && login_pass.length > 0){
        $(".login_button").css("background","#f14958");
    }else{
        $(".login_button").css("background","#92a0ad");
    }
    //验证输入手机号
    $(".login_photo").keyup(function(){
        var login_photo = $(this).val();
        var login_pass = $(".login_pass").val();
        if((login_photo != "") && (zzText.test(login_photo) == true) && login_pass.length > 0){
            $(".login_button").css("background","#f14958");
        }else{
            $(".login_button").css("background","#92a0ad");
        }
    });
    //验证输入密码
    $(".login_pass").keyup(function(){
        var login_photo = $(".login_photo").val();
        var login_pass = $(this).val();
        if((login_photo != "") && (zzText.test(login_photo) == true) && login_pass.length > 0){
            $(".login_button").css("background","#f14958");
        }else{
            $(".login_button").css("background","#92a0ad");
        }
    });
    //验证手机号真伪
    $(".login_button").tap(function(){
        var login_photo = $(".login_photo").val();
        var login_pass = $(".login_pass").val();
        var checkbox = $("input[type='checkbox']").attr('checked');
        if(login_pass == "" && login_photo == ""){
            Ddivok1("手机号和密码不能为空");
        }else if(login_pass == "" || login_pass.length < 6 || login_pass.length > 15){
            Ddivok1("请正确输入6-15密码");
        }else if((login_photo != "") && (zzText.test(login_photo) == true)){
            if(checkbox == true){
                localStorage.setItem("login_photo",login_photo);
                localStorage.setItem("login_pass",login_pass);
            }else{
                localStorage.setItem("login_photo","");
                localStorage.setItem("login_pass","");
            }
            formAjaxSubmit();
        }else{
            Ddivok1("请输入正确的手机号");
        }
    });

    /* 重置密码 */
    //判断验证码按钮和验证输入手机号
    $(".reset_photo").keyup(function(){
        var reset_yzm = $(".reset_yzm").val();
        var reset_pass = $(".reset_pass").val();
        var reset_yespass = $(".reset_yespass").val();
        var reset_photo = $(this).val();
        if((reset_photo != "") && (zzText.test(reset_photo) == true) && reset_pass.length > 0 && reset_yespass.length > 0 && reset_yzm.length > 0){
            $(".reset_button").css("background","#f14958");
        }else{
            $(".reset_button").css("background","#92a0ad");
        }
        if((reset_photo != "") && (zzText.test(reset_photo) == true)){
            $(".reset_yzm_button").attr("inputType",1).css("background","#f14958");
        }else{
            $(".reset_yzm_button").attr("inputType",0).css("background","#92a0ad");
        }
    });
    //发送验证码
    $(".reset_yzm_button").tap(function(){
        var yzm_inputType = $(".reset_yzm_button").attr("inputType");
        var reset_photo = $(".reset_photo").val();
        if((reset_photo != "") && (zzText.test(reset_photo) == true)){
            var delete_display = $(".regyzm").css("display");
            if(delete_display == "none" && yzm_inputType == 1){
                $(".regyzm").show();
                $('.verify').attr('src',httpHost + "/Public/verify?" + Math.random());
                $(".verify").tap(function(){
                    $('.verify').attr('src',httpHost + "/Public/verify?" + Math.random());
                });
                $("#regyzm-true").off("tap").on("tap",function(){
                    $(".loading_style").show();
                    var regyzmtext = $(".regyzmtext").val();
                    var mytime = new Date();
                    var keytime = (mytime.getMonth()+1)+""+mytime.getDate();
                    if(regyzmtext.length <= 0){
                        Ddivok1("请正确输入验证码");
                        $(".loading_style").hide();
                    }else{
                        if(yzm_inputType == 1){
                            var sucessWait = 60;
                            $.ajax({
                                type:"get",
                                url: httpHost+"/Public/getPhoneCode?registerTel=" + reset_photo + "&verify=" + regyzmtext + "&key=" + keytime + "&type=reset&t=" + Math.random(),
                                success: function(data){
                                    if (data.status == 0 && data.url == 1) {
                                        Ddivok1(data.info);
                                        $(".loading_style").hide();
                                        $('.verify').attr('src',httpHost + "/Public/verify?" + Math.random());
                                    }else if (data.status == 0) {
                                        Ddivok1(data.info);
                                        $(".regyzm").hide();
                                        $(".loading_style").hide();
                                        $(".regyzmtext").val("");
                                    }else if(data.status == 1){
                                        $(".regyzm").hide();
                                        $(".loading_style").hide();
                                        $(".regyzmtext").val("");
                                        $(".reset_yzm_button").attr("inputType",0);
                                        reg_time = setInterval(function(){
                                            $(".reset_yzm_button").text('已发送' + sucessWait + 's');
                                            if (sucessWait == -1) {
                                                $(".reset_yzm_button").text("重新获取");
                                                $(".reset_yzm_button").attr("inputType",1);
                                                clearInterval(reg_time);
                                            }
                                            sucessWait--;
                                        },1000);
                                    }
                                },
                                error: function(e) {
                                    Ddivok1(e);
                                }
                            });
                        }
                    }
                });
            }
        }else{
            Ddivok1("请输入正确的手机号");
        }
    });
    //验证输入验证码
    $(".reset_yzm").keyup(function(){
        var reset_photo = $(".reset_photo").val();
        var reset_pass = $(".reset_pass").val();
        var reset_yespass = $(".reset_yespass").val();
        var reset_yzm = $(this).val();
        if((reset_photo != "") && (zzText.test(reset_photo) == true) && reset_pass.length > 0 && reset_yespass.length > 0 && reset_yzm.length > 0){
            $(".reset_button").css("background","#f14958");
        }else{
            $(".reset_button").css("background","#92a0ad");
        }
    });
    //验证输入密码
    $(".reset_pass").keyup(function(){
        var reset_photo = $(".reset_photo").val();
        var reset_yzm = $(".reset_yzm").val();
        var reset_yespass = $(".reset_yespass").val();
        var reset_pass = $(this).val();
        if((reset_photo != "") && (zzText.test(reset_photo) == true) && reset_pass.length > 0 && reset_yespass.length > 0 && reset_yzm.length > 0){
            $(".reset_button").css("background","#f14958");
        }else{
            $(".reset_button").css("background","#92a0ad");
        }
    });
    //验证输入确认密码
    $(".reset_yespass").keyup(function(){
        var reset_photo = $(".reset_photo").val();
        var reset_yzm = $(".reset_yzm").val();
        var reset_pass = $(".reset_pass").val();
        var reset_yespass = $(this).val();
        if((reset_photo != "") && (zzText.test(reset_photo) == true) && reset_pass.length > 0 && reset_yespass.length > 0 && reset_yzm.length > 0){
            $(".reset_button").css("background","#f14958");
        }else{
            $(".reset_button").css("background","#92a0ad");
        }
    });
    //确认重置密码提交
    $(".reset_button").tap(function(){
        var reset_photo = $(".reset_photo").val();
        var reset_yzm = $(".reset_yzm").val();
        var reset_pass = $(".reset_pass").val();
        var reset_yespass = $(".reset_yespass").val();
        if(reset_photo == ""){
            Ddivok1("手机号码不能为空");
        }else if(reset_yzm == ""){
            Ddivok1("验证码不能为空");
        }else if(reset_pass == "" || reset_pass.length < 6 || reset_pass.length > 15 || reset_yespass == "" || reset_yespass.length < 6 || reset_yespass.length > 15){
            Ddivok1("请正确输入6-15密码");
        }else if((reset_photo != "") && (zzText.test(reset_photo) == true)){
            formAjaxSubmit();
        }else{
            Ddivok1("请输入正确的手机号");
        }
    });

    /* 注册 */
    if($(".register_number").val() != ""){
        $(".register_number").attr("readonly","readonly");
    }else{
        $(".register_number").removeAttr("readonly");
    }
    //判断验证码按钮和验证输入手机号
    $(".register_photo").keyup(function(){
        var register_yzm = $(".register_yzm").val();
        var register_pass = $(".register_pass").val();
        var register_yespass = $(".register_yespass").val();
        var register_number = $(".register_number").val();
        var register_photo = $(this).val();
        if((register_photo != "") && (zzText.test(register_photo) == true) && register_pass.length > 0 && register_yespass.length > 0 && register_yzm.length > 0 && register_number.length > 0){
            $(".register_button").css("background","#f14958");
        }else{
            $(".register_button").css("background","#92a0ad");
        }
        if((register_photo != "") && (zzText.test(register_photo) == true)){
            $(".register_yzm_button").attr("inputType",1).css("background","#f14958");
        }else{
            $(".register_yzm_button").attr("inputType",0).css("background","#92a0ad");
        }
    });
    //发送验证码
    $(".register_yzm_button").tap(function(){
        var yzm_inputType = $(".register_yzm_button").attr("inputType");
        var register_photo = $(".register_photo").val();
        if((register_photo != "") && (zzText.test(register_photo) == true)){
            var delete_display = $(".regyzm").css("display");
            if(delete_display == "none" && yzm_inputType == 1){
                $(".regyzm").show();
                $('.verify').attr('src',httpHost + "/Public/verify?" + Math.random());
                $(".verify").tap(function(){
                    $('.verify').attr('src',httpHost + "/Public/verify?" + Math.random());
                });
                $("#regyzm-true").off("tap").on("tap",function(){
                    $(".loading_style").show();
                    var regyzmtext = $(".regyzmtext").val();
                    var mytime = new Date();
                    var keytime = (mytime.getMonth()+1)+""+mytime.getDate();
                    if(regyzmtext.length <= 0){
                        Ddivok1("请正确输入验证码");
                        $(".loading_style").hide();
                    }else{
                        if(yzm_inputType == 1){
                            var sucessWait = 60;
                            $.ajax({
                                type:"get",
                                url: httpHost+"/Public/getPhoneCode?registerTel=" + register_photo + "&verify=" + regyzmtext + "&key=" + keytime + "&type=register&t=" + Math.random(),
                                success: function(data){
                                    if (data.status == 0 && data.url == 1) {
                                        Ddivok1(data.info);
                                        $(".loading_style").hide();
                                        $('.verify').attr('src',httpHost + "/Public/verify?" + Math.random());
                                    }else if (data.status == 0) {
                                        Ddivok1(data.info);
                                        $(".regyzm").hide();
                                        $(".loading_style").hide();
                                        $(".regyzmtext").val("");
                                    }else if(data.status == 1){
                                        $(".regyzm").hide();
                                        $(".loading_style").hide();
                                        $(".regyzmtext").val("");
                                        $(".register_yzm_button").attr("inputType",0);
                                        reg_time = setInterval(function(){
                                            $(".register_yzm_button").text('已发送' + sucessWait + 's');
                                            if (sucessWait == -1) {
                                                $(".register_yzm_button").text("重新获取");
                                                $(".register_yzm_button").attr("inputType",1);
                                                clearInterval(reg_time);
                                            }
                                            sucessWait--;
                                        },1000);
                                    }
                                },
                                error: function(e) {
                                    Ddivok1(e);
                                }
                            });
                        }
                    }
                });
            }
        }else{
            Ddivok1("请输入正确的手机号");
        }
    });
    //验证输入验证码
    $(".register_yzm").keyup(function(){
        var register_photo = $(".register_photo").val();
        var register_pass = $(".register_pass").val();
        var register_yespass = $(".register_yespass").val();
        var register_number = $(".register_number").val();
        var register_yzm = $(this).val();
        if((register_photo != "") && (zzText.test(register_photo) == true) && register_pass.length > 0 && register_yespass.length > 0 && register_yzm.length > 0 && register_number.length > 0){
            $(".register_button").css("background","#f14958");
        }else{
            $(".register_button").css("background","#92a0ad");
        }
    });
    //验证输入密码
    $(".register_pass").keyup(function(){
        var register_yzm = $(".register_yzm").val();
        var register_photo = $(".register_photo").val();
        var register_yespass = $(".register_yespass").val();
        var register_number = $(".register_number").val();
        var register_pass = $(this).val();
        if((register_photo != "") && (zzText.test(register_photo) == true) && register_pass.length > 0 && register_yespass.length > 0 && register_yzm.length > 0 && register_number.length > 0){
            $(".register_button").css("background","#f14958");
        }else{
            $(".register_button").css("background","#92a0ad");
        }
    });
    //验证输入确认密码
    $(".register_yespass").keyup(function(){
        var register_yzm = $(".register_yzm").val();
        var register_photo = $(".register_photo").val();
        var register_pass = $(".register_pass").val();
        var register_number = $(".register_number").val();
        var register_yespass = $(this).val();
        if((register_photo != "") && (zzText.test(register_photo) == true) && register_pass.length > 0 && register_yespass.length > 0 && register_yzm.length > 0 && register_number.length > 0){
            $(".register_button").css("background","#f14958");
        }else{
            $(".register_button").css("background","#92a0ad");
        }
    });
    //服务商号
    $(".register_number").keyup(function(){
        var server_texts = $(this).val();
        var register_yzm = $(".register_yzm").val();
        var register_photo = $(".register_photo").val();
        var register_pass = $(".register_pass").val();
        var register_yespass = $(".register_yespass").val();
        if((register_photo != "") && (zzText.test(register_photo) == true) && register_pass.length > 0 && register_yespass.length > 0 && register_yzm.length > 0 && server_texts.length > 0){
            $(".register_button").css("background","#f14958");
            $('.agent_id').val(server_texts);
        }else{
            $(".register_button").css("background","#92a0ad");
        }
    });
    //$(".register_number_none").tap(function(){
    //    $(".server_show_news li").remove();
    //    var display_server = $(".server_show").css("display");
    //    if(display_server == "none"){
    //        $(".opa_background").show();
    //        $(".server_show").show();
    //        $.ajax({
    //            type:"get",
    //            url:httpHost+"/Register/agentList",
    //            success:function(data){
    //                for(var i=0; i<data.list.length; i++){
    //                    var Dhtml = '<li id="' + data.list[i].no + '">' + data.list[i].no + '</li>';
    //                    $(".server_show_news ul").append(Dhtml);
    //                }
    //                $(".server_show_news li").tap(function(){
    //                    var server_texts = $(this).text();
    //                    var server_ids = $(this).attr("id");
    //                    $('.agent_id').val(server_ids);
    //                    $(".register_number").val(server_texts);
    //                    $(".opa_background").hide();
    //                    $(".server_show").hide();
    //                    var register_yzm = $(".register_yzm").val();
    //                    var register_photo = $(".register_photo").val();
    //                    var register_pass = $(".register_pass").val();
    //                    var register_yespass = $(".register_yespass").val();
    //                    var register_number = $(".register_number").val();
    //                    if((register_photo != "") && (zzText.test(register_photo) == true) && register_pass.length > 0 && register_yespass.length > 0 && register_yzm.length > 0 && register_number.length > 0){
    //                        $(".register_button").css("background","#f14958");
    //                    }else{
    //                        $(".register_button").css("background","#92a0ad");
    //                    }
    //                });
    //            }
    //        });
    //    }
    //});
    //确认注册提交
    $(".register_button").tap(function(){
        var register_yzm = $(".register_yzm").val();
        var register_photo = $(".register_photo").val();
        var register_pass = $(".register_pass").val();
        var register_yespass = $(".register_yespass").val();
        if(register_photo == ""){
            Ddivok1("手机号码不能为空");
        }else if(register_yzm == ""){
            Ddivok1("验证码不能为空");
        }else if(register_pass == "" || register_pass.length < 6 || register_pass.length > 15 || register_yespass == "" || register_yespass.length < 6 || register_yespass.length > 15){
            Ddivok1("请正确输入6-15密码");
        }else if($(".register_number").val().length <= 0 || RegExp(/^\d+$/g).test($(".register_number").val()) == false){
            Ddivok1("请正确输入服务商编号");
        }else if($(".reg_server_text").attr("texttype") == 0){
            Ddivok1("请认真阅读服务协议");
        }else if((register_photo != "") && (zzText.test(register_photo) == true)){
            formAjaxSubmit();
        }else{
            Ddivok1("请输入正确的手机号");
        }
    });
    //服务协议
    $(".server_tap").tap(function(){
        var server_display = $(".server_text").css("display");
        if(server_display == "none"){
            $(".opa_background").show();
            $(".server_text").show();
        }
    });
    $(".opa_background").tap(function(){
        $(".opa_background").hide();
        $(".server_text").hide();
        $(".server_show").hide();
    });
    $(".server_block_style input").tap(function(){
        var a=$(".server_block_style").css("display");
        if(a!="none"){
            $(".opa_background").hide();
            $(".server_block_style").hide()
        }
    });

    /* 完成用户信息 */
    $(".mate_radio span").tap(function(){
        var Dtext = $(this).find("input").val();
        if(Dtext == "people"){
            $(".institution").hide();
            $(".mate_style ul input").val("");
        }else{
            $(".institution").show();
            $(".mate_style ul input").val("");
        }
        $(".mate_button").val("完善用户信息");
    });
    var mateNum = 0;
    $(".mate_button").tap(function(){
        isLogin();
        var Dtext = $(".mate_radio input[name='Dradio']:checked").attr('value');
        var mate_jigou = $(".mate_jigou").val();
        var mate_daima = $(".mate_daima").val();
        var mate_name = $(".mate_name").val();
        var mate_identity = $(".mate_identity").val();
        if(Dtext == "people"){
            if(mate_name == ""){
                Ddivok1("姓名不能为空");
            }else if(mate_identity == ""){
                Ddivok1("身份证号不能为空");
            }else{
                if(mateNum == 0){
                    mateNum = 1;
                    $(".loading_style").show();
                    $.ajax({
                        type:"get",
                        url: httpHost+"/User/doSaveUserInfo?user_type=" + 1 + "&person_name=" + mate_name + "&id_no=" + mate_identity,
                        success: function(data){
                            if(data.status == 1){
                                $(".loading_style").hide();
                                Ddivok1(data.info);
                                setTimeout(function(){
                                    location.href = data.url;
                                },1500);
                            }else{
                                $(".loading_style").hide();
                                Ddivok1(data.info);
                                setTimeout(function(){
                                    mateNum = 0;
                                },1500);
                            }
                        },
                        error: function(e) {
                            Ddivok1(e);
                        }
                    });
                }
            }
        }else{
            if(mate_jigou == ""){
                Ddivok1("机构名称不能为空");
            }else if(mate_daima == ""){
                Ddivok1("组织机构代码不能为空");
            }else if(mate_name == ""){
                Ddivok1("姓名不能为空");
            }else if(mate_identity == ""){
                Ddivok1("身份证号不能为空");
            }else{
                if(mateNum == 0){
                    mateNum = 1;
                    $(".loading_style").show();
                    $.ajax({
                        type:"get",
                        url: httpHost+"/User/doSaveUserInfo?user_type=" + 2 + "&person_name=" + mate_name + "&id_no=" + mate_identity + "&company_name=" + mate_jigou + "&license_no=" + mate_daima,
                        success: function(data){
                            console.log(data);
                            if(data.status == 1){
                                $(".loading_style").hide();
                                Ddivok1(data.info);
                                setTimeout(function(){
                                    location.href = data.url;
                                },1500);
                            }else{
                                $(".loading_style").hide();
                                Ddivok1(data.info);
                                setTimeout(function(){
                                    mateNum = 0;
                                },1500);
                            }
                        },
                        error: function(e) {
                            Ddivok1(e);
                        }
                    });
                }
            }
        }
    });
});
